#include "config.h"

#include <clx/libECM.h>
#include <clx/ecm_list.h>

#include "blockcache.h"

static LIST_HEAD(lru_list);

typedef struct {
	bcache_t	*bcache;
	struct list_head	list;
} priv_t;

static const char *
lru_init(ecm_uint32 cache_blksize, ecm_uint32 n_caches)
{
	return "LRU";
}

static void
lru_create(bcache_t *bcache)
{
	priv_t	*priv;

	priv = (priv_t *)ecm_malloc(sizeof(priv_t));
	bcache->priv = priv;

	priv->bcache = bcache;
	list_add(&priv->list, &lru_list);
}

static void 
lru_hit(bcache_t *bcache)
{
	priv_t	*priv = (priv_t *)bcache->priv;
	
	list_del_init(&priv->list);
}

static bcache_t *
lru_replace(ecm_uint32 mid, ecm_uint32 bid)
{
	priv_t	*priv;

	if (list_empty(&lru_list))
		return NULL;

	priv = list_entry(lru_list.next, priv_t, list);
	list_del_init(&priv->list);
	return priv->bcache;
}

static void
lru_put(ecm_uint32 sid, bcache_t *bcache)
{
	priv_t	*priv = (priv_t *)bcache->priv;

	list_add_tail(&priv->list, &lru_list);
}

static void
lru_access(ecm_uint32 sid, ecm_uint32 mid, ecm_uint32 bid)
{
}

static void
lru_leave(ecm_uint32 sid)
{
}

static void
lru_add_media(ecm_uint32 bid_max)
{
}

static void
lru_fini(void)
{
}

blockcache_op_t	bcache_op = {
	lru_init,
	NULL,
	NULL,
	lru_add_media,

	lru_create,
	lru_hit,
	lru_replace,
	lru_put,
	
	lru_access,
	lru_leave,
	lru_fini
};
